<?php

namespace Modules\Form\Controllers;

use Modules\Form\Library\ValidateCode;
use Core\Mvc\Controller;

class IndexController extends Controller
{
    public function validateCodeAction()
    {
        self::$variables = array(
            '#templates' => 'validate',
        );
        $validate = new ValidateCode();
        $validate->doimg();
        $this->session->set('code', $validate->getCode());
    }

    public function autoinput($id)
    {
        self::$variables['#templates'] = 'json';
        $autoinput = $this->config->cache('autoinput');
        $data = [];
        if (isset($autoinput[$id])) {
            $autoinputInfo = $autoinput[$id];
            if (isCallable($autoinputInfo['callable'])) {
                $data = call_user_func($autoinputInfo['callable']);
            }
        }
        self::$variables['data'] = $data;
    }

    public function functionValidationAction($callable)
    {
        self::$variables['#templates'] = 'json';
        if (isCallable($callable)) {
            self::$variables['data'] = call_user_func($callable);
        } else {
            self::$variables['data'] = false;
        }
    }

    public function chosenSourceAction($id)
    {
        $data = [];
        $chosenSourceList = $this->config->cache('chosenSource');
        if (!$this->request->isAjax() || !isset($chosenSourceList[$id])) {
            return $this->notFount();
        }

        $chosenSource = $chosenSourceList[$id];
        $chosenSourceClass = $chosenSource['class'] . '::ajaxSource';

        if (isCallable($chosenSourceClass)) {
            $data = call_user_func($chosenSourceClass);
        }

        self::$variables['#templates'] = 'json';
        self::$variables['data'] = $data;
    }
}
